import FormModal from '@/components/FormModal/index.jsx';
import { Form, Input, Radio, Select, Switch } from 'antd';
import { forwardRef, useImperativeHandle, useRef, useState } from 'react';
import ModalUtil from '@/utils/modal.js';
import { StorageApi } from '@/api/storage/api.js';
import StorageConfigEdit from '@/pages/StorageManage/widget/StorageConfigEdit/index.jsx';

function StorageFormModal({ onRefresh }, ref) {
  useImperativeHandle(ref, () => ({
    openEdit: (data) => openEdit(data),
    openAdd: () => openAdd(),
    close: () => formRef.current?.close()
  }));

  const [isEdit, setIsEdit] = useState(false);
  const formRef = useRef();
  const [form] = Form.useForm();
  const [updateId, setUpdateId] = useState();
  const serviceProviderValue = Form.useWatch('serviceProvider', form);

  // 打开编辑弹窗
  function openEdit(data) {
    setIsEdit(true);
    if (data?.storageId == null) {
      setIsEdit(false);
      ModalUtil.error('数据ID丢失，请刷新重试');
      return;
    }
    setUpdateId(data.storageId);
    formRef.current?.open();
    form.setFieldsValue(data);
  }

  // 打开新增弹窗
  function openAdd() {
    setIsEdit(false);
    formRef.current?.open();
    form?.resetFields();
  }

  // 提交表单
  const handleSubmit = async (values) => {
    let isSuccess = false;
    if (isEdit) {
      const { code } = await StorageApi.update(updateId, values);
      if (code === 0) {
        isSuccess = true;
      }
    } else {
      const { code } = await StorageApi.add(values);
      if (code === 0) {
        isSuccess = true;
      }
    }
    if (isSuccess) {
      ModalUtil.success('操作成功');
      form?.resetFields();
      onRefresh && onRefresh();
      formRef.current?.close();
    }
  };

  return (
    <FormModal
      form={form}
      width={600}
      ref={formRef}
      name="storageForm"
      title={isEdit ? '编辑存储配置' : '新增存储配置'}
      onFinish={handleSubmit}
      formItems={
        <>
          <Form.Item
            name="storageName"
            label="存储名称"
            rules={[{ required: true, message: '请输入存储名称' }]}
          >
            <Input />
          </Form.Item>
          <Form.Item
            name="storageDesc"
            label="存储说明"
            rules={[{ required: true, message: '请输入存储说明' }]}
          >
            <Input.TextArea />
          </Form.Item>
          <Form.Item
            name="storageConfig"
            label="存储配置"
            rules={[{ required: true, message: '请选择存储配置' }]}
            initialValue={{}}
          >
            <StorageConfigEdit serviceProvider={serviceProviderValue} />
          </Form.Item>
          <Form.Item
            name="serviceProvider"
            label="服务商"
            rules={[{ required: true, message: '请选择服务商' }]}
          >
            <Select
              placeholder="请选择服务商"
              options={[
                { label: '本地存储(local)', value: 'local' },
                { label: 'Minio(minio)', value: 'minio' }
              ]}
            />
          </Form.Item>
          <Form.Item
            name="status"
            label="状态"
            rules={[{ required: true, message: '请选择状态' }]}
            initialValue="1"
          >
            <Radio.Group
              options={[
                { label: '启用', value: '1' },
                { label: '禁用', value: '2' }
              ]}
              optionType="button"
              buttonStyle="solid"
            />
          </Form.Item>
          <Form.Item name="isDefault" label="是否默认存储" initialValue={false}>
            <Switch />
          </Form.Item>
        </>
      }
    />
  );
}

export default forwardRef(StorageFormModal);
